<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:11
 */
class Level extends MY_Admin_Controller
{
    public function index()
    {

        $this->load->model('user/Model_user_level_config', 'level');
        $list = $this->level->getAllLists();

        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getAllLists();

        $data = array(
            'infoAll' => $list,
            'fileInfo'=> $fileInfo
        );
        $this->load->view('admin/user/level/index', $data);
    }

    public function create()
    {

        $lid = (int)$this->uri->segment(4);

        //推荐人列表
        $this->load->model('user/Model_user_level_config','level');
        $levelInfo = $this->level->getById($lid);

        $this->load->model('common/Model_file','file');
        $fileInfo  = $this->file->getById($levelInfo['scale']);

        $data = array(
            'levelInfo'       =>    $levelInfo,
            'fileInfo'        =>    $fileInfo,
        );

        $this->load->view('admin/user/level/create',$data);
    }

    public function save()
    {
        $lid = $this->input->get_post('lid');

        $data = array(
            'name'  => trim($this->input->get_post('levelName')),
            //判断是否选择图片如果选择了图片则获取新突破地址或者判断是新建页面还是修改页面
            'scale'           =>     !empty($_FILES['up_file']['tmp_name'])?$this->uploadFile('up_file'):(isset($lid)?$this->input->get_post('pic'):''),
            'descr' => trim($this->input->get_post('levelDescr')),
            'term'  => trim($this->input->get_post('levelReq')),
        );

        if (!$lid) {
            $data['create_time'] = date("Y-m-d H:i:s", time());
        }

        $this->load->model('user/Model_user_level_config', 'level');
        $this->level->save($data, $lid);

        $this->load->helper('url');
        redirect(url('admin') . 'user/level/index', 'refresh');

    }

    /**
     * 删除用户等级
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $lid = (int)$this->input->get_post('lid');

            if (!$lid)
            {
                $response = error(200001);
                break;
            }

            $this->load->model('user/model_user', 'user');

            $this->load->model('user/model_user_level_config', 'level');
            $res = $this->level->delete(array('lid' => $lid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}